package com.example.qimo_weather

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import com.squareup.picasso.Picasso
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MainActivity : AppCompatActivity() {

    private lateinit var weatherApiService: WeatherApiService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)

        val weatherTextView = findViewById<TextView>(R.id.weatherTextView)
        val weatherIconImageView = findViewById<ImageView>(R.id.weatherIconImageView)
        val temperatureTextView = findViewById<TextView>(R.id.temperatureTextView)
        val humidityTextView = findViewById<TextView>(R.id.humidityTextView)
        val descriptionTextView = findViewById<TextView>(R.id.descriptionTextView)

        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.openweathermap.org/data/2.5/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        weatherApiService = retrofit.create(WeatherApiService::class.java)

        fetchWeather("Beijing", "09b76ac2856af4c46d5ccce99ac4b8f5")
    }

    private fun fetchWeather(city: String, apiKey: String) {
        weatherApiService.getWeather(city, apiKey).enqueue(object : Callback<WeatherResponse> {
            override fun onResponse(call: Call<WeatherResponse>, response: Response<WeatherResponse>) {
                if (response.isSuccessful) {
                    val weatherResponse = response.body()
                    val weather = weatherResponse?.weather?.firstOrNull()
                    val main = weatherResponse?.main

                    findViewById<TextView>(R.id.weatherTextView).text = "City: ${weatherResponse?.name}"
                    findViewById<TextView>(R.id.temperatureTextView).text = "Temperature: ${main?.temp}°C"
                    findViewById<TextView>(R.id.humidityTextView).text = "Humidity: ${main?.humidity}%"
                    findViewById<TextView>(R.id.descriptionTextView).text = "Description: ${weather?.description}"

                    weather?.let {
                        val weatherIconUrl = "https://openweathermap.org/img/wn/${it.icon}@2x.png"
                        Picasso.get().load(weatherIconUrl).into(findViewById<ImageView>(R.id.weatherIconImageView))
                    }
                } else {
                    Log.e("WeatherApp", "Error: ${response.message()}")
                }
            }

            override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {
                Log.e("WeatherApp", "Failure: ${t.message}")
            }
        })
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_day_night -> {
                if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                }
                true
            }
            R.id.action_second_activity -> {
                startActivity(Intent(this, SecondActivity::class.java))
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}